import fse from 'fs-extra';
import path from 'path';
import { docDir } from './../util/settings';
import {PicInfoDao} from "./PicInfoDao";
// 将数据存至系统用户目录，防止用户误删程序
export const dbPath = path.join(docDir, 'data.sqlite3');
fse.ensureFileSync(dbPath);
var Conn = require('./../base/dbConn3.js');

// sqlites数据库地址
var file = dbPath;

// links the database
var links = null;

/**
 * { 构造函数 }
 */
function MenuInfoDao() {
    console.log('MenuInfoDao.constructor');
}

/**
 * Links a data base.
 */
MenuInfoDao.prototype.linkDataBase = function() {
    console.dir(links)
    console.info(links.toString())
};

/**
 * 保存菜单
 * saveMenuInfo
 *
 * @param      {<type>}  params  The parameters
 * @return     {<type>}  { description_of_the_return_value }
 */
MenuInfoDao.prototype.saveMenuInfo = function(params) {
    console.dir(params)
    var props = {}

    for (let i in params) {
        if (params[i] == null || undefined || '') {
            this.errorMsg = '参数不齐全!';
            console.error(this.errorMsg)
            return this.errorMsg;
        }
    }
    props.sql = 'INSERT INTO menu_info '
    props.sql += '(menu_name,parent_id,create_time) '
    props.sql += ' VALUES'
    props.sql += ' (?,?,?);';

    props.value = params;
    console.log('savemenuInfo+sql:::',props);

    try {
        links =new Conn.databaseCon(file);
        links.insertPlus(props);
    } catch (error) {
        console.error(error)
        throw error;
    } finally {
        links.closeConnect();
    }
};

/**
 * 根据一个或多个id删除一或多行菜单
 *删除菜单
 * @param      {<type>}  props   The properties
 * @return     {<type>}  { description_of_the_return_value }
 */
MenuInfoDao.prototype.deleteByIds = function(props) {
    console.dir(props);
    var sql = "DELETE FROM menu_info WHERE ";
    sql += props.column
    sql += " IN "
    sql += " ( "
    sql += props.value.join(", ");
    sql += " ) "
    sql += " ;";
    console.log('deleteByIds+sql:::',sql);

    try {
        links =new Conn.databaseCon(file);
        links.deletes(sql);
    } catch (err) {
        console.error(err);
        return err;
    }
};


/**
 * { 根据id修改菜单 }
 *
 * @param      {<type>}  user    The user
 * @return     {<type>}  { description_of_the_return_value }
 */
MenuInfoDao.prototype.updatesById = function(menuInfo) {
    if (menuInfo == null || '' || undefined) {
        this.errorMsg = '参数不齐全!';
        console.error(this.errorMsg)
        throw this.errorMsg;
    }
    var sqlSentence = "UPDATE menu_info SET ";

    if (menuInfo.menuName != null || '' || undefined) {
        sqlSentence += " menu_name = ";
        sqlSentence += "'";
        sqlSentence += menuInfo.menuName;
        sqlSentence += "'";
        if (menuInfo.parentId != null || '' || undefined) {
            sqlSentence += ",";
        }
    }

    if (menuInfo.parentId != null || '' || undefined) {
        sqlSentence += "parent_id = ";
        sqlSentence += "'";
        sqlSentence += menuInfo.parentId;
        sqlSentence += "' ";
    }

    if (menuInfo.id == null || '' || undefined) {
        this.errorMsg = '参数[id]为空!';
        console.error(this.errorMsg)
        throw this.errorMsg;
    }
    sqlSentence += " WHERE id = ";
    sqlSentence += menuInfo.id;
    sqlSentence += " ;";
    try {
        links =new Conn.databaseCon(file);
        links.executeCommand(sqlSentence);
    } catch (error) {
        console.error(error)
        throw error;
    } finally {
        links.closeConnect();
    }
};

/**
 * 获取菜单列表
 *
 * @return     {<type>}  The user list.
 */
MenuInfoDao.prototype.getMenuInfoList = function() {
    var querySql = "SELECT ";
    querySql += "id,menu_name,parent_id,create_time "
    querySql += "FROM menu_info";
    querySql += ";";
    links =new Conn.databaseCon(file);
    this.promiseObj = links.findAll(querySql).then(value => {
        console.log('成功态,接受resolved的结果...')
        console.dir(JSON.stringify(value));
        return value;
    }, value => {
        console.log('失败态,接受rejected的结果:')
        console.dir(JSON.stringify(value));
    })
    return this.promiseObj;
};

MenuInfoDao.prototype.getMenuByNoParam = function(params) {
    var querySql = "SELECT ";
    querySql += "id,menu_name,parent_id,create_time "
    querySql += "FROM menu_info";
    querySql += "id "
    querySql += " IN "
    querySql += " ( "
    querySql += params.join(", ");
    querySql += " ) "
    querySql += ";";
    links =new Conn.databaseCon(file);
    this.promiseObj = links.findByFieldValue(querySql);
    console.log('getMenuAndPicByMenuIdgetMenuAndPicByMenuIdgetMenuAndPicByMenuId',this.promiseObj);
    return this.promiseObj;
};

MenuInfoDao.prototype.getAllMenu = function() {
    var sql = "select id,menu_name,parent_id,0 as type,create_time as createTime from menu_info "
    sql +=  " ORDER BY createTime desc "
    links =new Conn.databaseCon(file);
    this.promiseObj = links.findByFieldValue(sql);
    return this.promiseObj;
};


//导出模块给vue
export {
    MenuInfoDao
}
